package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.IpLog;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.mapper.LoginInfoMapper;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IIpLogService;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.*;

import cn.wolfcode.p2p.base.vo.VerifyCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by wolfcode on 2018/7/20.
 */
@Service
@Transactional
public class LoginInfoServiceImpl implements ILoginInfoService {
	@Autowired
	private LoginInfoMapper loginInfoMapper;
	@Autowired
	private IAccountService accountService;
	@Autowired
	private IUserInfoService userInfoService;
	@Autowired
	private IIpLogService ipLogService;

	public void register(String username, String verifycode, String password, String confirmPwd, HttpSession session) {
		AssertUtil.isNotNull(username, "用户名不能为空!");
		AssertUtil.isNotNull(verifycode, "验证码不能为空!");
		AssertUtil.isNotNull(password, "密码不能为空!");
		AssertUtil.isNotNull(confirmPwd, "确认密码不能为空!");
		if (!password.equals(confirmPwd)) {
			throw new DisplayableException("两次密码输入不一致");
		}
		// 发送验证码是否正确.TODO
		// 从session中获取vo对象,判断验证码是否有效,时间是否在有效期之内,手机号码和发送短信的手机号码一致
		VerifyCodeVo vo = (VerifyCodeVo) session.getAttribute("VERIFYCODE_IN_SESSION");
		if (vo != null && vo.getRandomCode().equalsIgnoreCase(verifycode) && vo.getPhoneNumber().equals(username)
				&& DateUtil.getTimeBetween(vo.getSendTime(), new Date()) < Constants.VERIFYCODE_VAILD_TIME * 60) {
			// 用户名是否存在
			int count = loginInfoMapper.queryCountByUserName(username);
			if (count > 0) {
				throw new DisplayableException("用户名已经存在!");
			}
			// 把数据保存入库
			LoginInfo loginInfo = new LoginInfo();
			loginInfo.setUsername(username);
			loginInfo.setPassword(MD5.encode(password));
			loginInfo.setUserType(LoginInfo.TYPE_NORMAL);
			loginInfoMapper.insert(loginInfo);
			// 把userinfo和Account对象创建出来
			Account account = new Account();
			account.setId(loginInfo.getId());
			accountService.save(account);
			UserInfo userInfo = new UserInfo();
			userInfo.setId(loginInfo.getId());
			userInfo.setPhoneNumber(username);
			userInfoService.save(userInfo);
		} else {
			throw new DisplayableException("验证码有误,请重新发送!");
		}
	}

	public Boolean checkPhoneNumber(String username) {
		/*
		 * int count = loginInfoMapper.queryCountByUserName(username);
		 * if(count<=0){ return true; }else{ return false; }
		 */
		return loginInfoMapper.queryCountByUserName(username) <= 0;
	}

	public LoginInfo login(String username, String password, int userType) {
		// 1.根据用户名和密码到数据库中查询的对象
		LoginInfo current = loginInfoMapper.login(username, MD5.encode(password), userType);
		IpLog ipLog = new IpLog();
		ipLog.setLoginTime(new Date());
		ipLog.setUsername(username);
		ipLog.setUserType(userType);
		ipLog.setIp(UserContext.getIp());
		// 如果查不到,提示用户账号密码有误
		if (current == null) {
			ipLog.setState(IpLog.STATE_FAILED);
		} else {
			ipLog.setState(IpLog.STATE_SUCCESS);
			// 如果能查到对象,把对象放入session中.
			UserContext.setCurrent(current);
		}
		ipLogService.save(ipLog);
		return current;
	}

	@Override
	public List<LoginInfo> listAuditors() {
		return this.loginInfoMapper.listAuditors();
	}

}
